#include<bits/stdc++.h>
using namespace std;

int number[10000][200];
int stairs[10000][200];
int quan[10000],lm;
int main()
{
    int n,m,ans=0,xx;
    int lm;
    int i,ii;
    int j,jj;
    cin >> n >> m;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            cin >> stairs[i][j] >> number[i][j];
            quan[i]+=stairs[i][j];
        }
    }
    cin >> jj;
    for(ii=0;ii<n;ii++)
    {
        ans=ans+number[ii][jj];
        lm=number[ii][jj];
        lm=lm%quan[ii]+quan[ii];
        lm=lm-stairs[ii][jj];
        while(lm>0)
        {
            jj++;
            jj%=m;
            lm=lm-stairs[ii][jj];
        }
    }
	cout << ans % 20123 << endl;
    return 0;
}
